<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
use Mail;

class userLoginController extends Controller
{
    //前台登录首页
    public function login()
    {
    	return view('home.login');
    }

    //登录验证
    public function doLogin(Request $request)
    {
    	
        // 自定义错自误信息
        $message = [
            'required' => '不能为空',
        ];
        //设定验证规则
        $this->validate($request, [
            'userName' => 'required',
            'userPass' => 'required',
            'code' => 'required',
        ],$message);
        
        // 获取session中的mycode
        $mycode = session()->get('mycode');

        //判断用户输入的验证码和session是否相等
        if ( $mycode != $request->input('code') ) {
            return back()->with('mag','验证码错误');
        }
      
        //匹配数据库
        $ob = DB::table('storeuser')->where('userName',$request->input('userName'))->first();

        if ( $ob->userState != 1){
            return back()->with('msg','没有权限'); 
        }
       
        if( $ob  ) {

            //判断密码是否相等
            if ( $ob->userPass  == md5($request->input('userPass'))) {

                  session(['user'=> $ob]);
                  
                 return redirect('/');
            }

           
         }  
            return back()->with('msg','登录失败');  

         }  

    //退出登录
    public function exitLogin(Request $request)
    {   
        // dd(session('user'));
       $request->session()->forget('user');
        return redirect('/');
    }
            
}


